﻿using System;
using System.Collections.Generic;
using System.Text;
using FluorineFx;
using MySql.Data.MySqlClient;

namespace Could.Service
{
    [FluorineFx.RemotingService("Web Could  CheckDataServiceLibrary")]
    class CheckDataLib
    {
        public  CheckData Check_FocusIn( string CtlID){
            return null;
        }
        public CheckData Check_FocusOut(string CtlID , string value)
        {
            value = value.Trim();
            CheckData cd = new CheckData();
            cd.CtlID = CtlID;
            cd.status = false;
            cd.sMessage = "";
            string sql = "";
            try
            {
                if ( cd.CtlID == "1"){
                    if (value.Length <4 || value.Length >12)
                    {
                        cd.sMessage = "译员帐号长度超出范围,长度控制在 4-12 字节以内";
                        return cd;
                    }
                    cd.sMessage = "译员帐号已经存在！";
                    sql = "SELECT count(id) FROM tmscreators WHERE user_id='" + value + "'";
                }else if (cd.CtlID == "2"){
                    cd.sMessage = "译员的电子邮箱已经被注册过！";
                    sql = "SELECT count(id) FROM tmscreators WHERE user_email='" + value + "'";
                    string email = @"\w+([-+.]\w+)*@\w+([-.]\w+)*\.\w+([-.]\w+)*";
                    if (!System.Text.RegularExpressions.Regex.IsMatch(value.Trim(), email))
                    {
                        cd.sMessage = "译员电子邮箱地址不正确！";
                        return cd;
                    }
                }
                MySqlConnection connection = new MySqlConnection(BaseData.GetConnectionString());
                
                MySqlCommand command = new MySqlCommand(sql, connection);
                connection.Open();
                cd.status = Convert.ToInt32(command.ExecuteScalar()) == 0;
                if (cd.status){
                   cd.sMessage = "";
                }
            }catch( System.Exception ){}
            return cd;
        }
    }
}
